package com.atguigu.gmall.cart.rpc;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.config.interceptor.UserAuthContext;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.cart.entity.CartInfo;
import com.atguigu.gmall.model.cart.entity.UserAuth;
import com.atguigu.gmall.model.cart.vo.AddCartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/inner/cart")
public class CartRpcController {

    @Autowired
    CartService cartService;

    @Autowired
    UserAuthContext userAuthContext;

    @GetMapping("/addToCart")
    public Result<AddCartVO> addToCart(@RequestParam Long skuId,@RequestParam Integer skuNum) {
        UserAuth userAuth = userAuthContext.getUserAuth();
        String key = cartService.decisionKey(userAuth);
        AddCartVO addCartVO = cartService.addCart(key,skuId, skuNum);
        return Result.ok(addCartVO);
    }

    @GetMapping("/deleteChecked")
    public Result deleteChecked() {
        UserAuth userAuth = userAuthContext.getUserAuth();
        String key = cartService.decisionKey(userAuth);
        cartService.deleteChecked(key);
        return Result.ok();
    }

    /**
     * 获取购物车已选择的商品列表
     * @return
     */
    @GetMapping("/checkedList")
    public  Result<List<CartInfo>> checkedList() {
        UserAuth userAuth = userAuthContext.getUserAuth();
        String key = cartService.decisionKey(userAuth);
        List<CartInfo> cartInfoList = cartService.checkedList(key);
        return Result.ok(cartInfoList);
    }

}
